// Copyright 2024 Ant Group Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "psi/algorithm/spiral/gadget.h"

#include "gtest/gtest.h"

#include "psi/algorithm/spiral/util.h"

namespace psi::spiral::util {

namespace {
constexpr size_t kMaxLoop{100};
}

TEST(Gadget, BuildGadgetCorrect) {
  auto params = util::GetTestParam();
  auto shared_params = std::make_shared<Params>(params);

  auto g = util::BuildGadget(params, 1, 4);

  // ground truth comes from rust code
  ASSERT_EQ(g.Poly(0, 0)[0], 1);
  ASSERT_EQ(g.Poly(0, 1)[0], 32768);
  ASSERT_EQ(g.Poly(0, 2)[0], 1073741824);
  ASSERT_EQ(g.Poly(0, 3)[0], 35184372088832);

  // other value is 0
  for (size_t i = 0; i < g.Data().size(); ++i) {
    if (i == g.PolyStartIndex(0, 0) || i == g.PolyStartIndex(0, 1) ||
        i == g.PolyStartIndex(0, 2) || i == g.PolyStartIndex(0, 3)) {
      continue;
    }
    ASSERT_EQ(g.Data()[i], 0);
  }
}

TEST(UtilTest, BytesToCoeffs) {
  size_t logt = 20;
  size_t offset = 0;
  size_t size = 256;

  yacl::crypto::Prg<uint8_t> prg;
  std::vector<uint8_t> bytes(size);

  for (size_t i = 0; i < kMaxLoop; ++i) {
    prg.Fill(absl::MakeSpan(bytes));
    // convert to coeff
    auto coeffs = ConvertBytesToCoeffs(logt, offset, size, bytes);
    auto bytes_convert = ConvertCoeffsToBytes(coeffs, logt);
    // the length may be different, so we use span
    ASSERT_EQ(absl::MakeSpan(bytes),
              absl::MakeSpan(bytes_convert.data(), bytes.size()));
  }
}

TEST(UtilTest, ConvertBytesToCoeffs) {
  size_t logt = 20;
  size_t offset = 0;
  size_t size = 256;

  std::vector<uint8_t> bytes{
      0x57, 0xAB, 0xF0, 0x6C, 0x8C, 0x83, 0x25, 0xC1, 0x3C, 0x18, 0xB1, 0x15,
      0x25, 0xA5, 0xE8, 0x5E, 0xCD, 0xFB, 0x62, 0x94, 0xFB, 0x9E, 0x05, 0x72,
      0x39, 0x06, 0xC2, 0x6A, 0x8C, 0xFC, 0x6A, 0x5B, 0x65, 0xE3, 0x28, 0x4D,
      0x90, 0x6C, 0x4E, 0x31, 0x27, 0x8C, 0x57, 0xE0, 0x77, 0x86, 0x68, 0x62,
      0x0E, 0x2E, 0xFF, 0x40, 0xB0, 0xCA, 0x24, 0xD7, 0xE7, 0xC9, 0xEA, 0x4F,
      0xC5, 0x17, 0x3E, 0x4D, 0x20, 0xC1, 0x45, 0xD3, 0x44, 0xB9, 0x7B, 0xA9,
      0x4E, 0x37, 0xBD, 0x03, 0x91, 0xE9, 0xC8, 0x60, 0x07, 0xA8, 0xA8, 0x8E,
      0x52, 0x61, 0xA4, 0x67, 0xAD, 0xAD, 0xCA, 0x1D, 0xA2, 0x7A, 0x3E, 0x68,
      0x08, 0xFF, 0x3E, 0x19, 0x26, 0xBD, 0x15, 0xFB, 0x6B, 0x85, 0xF2, 0xF2,
      0x31, 0xA7, 0x9C, 0xC1, 0xA6, 0xEA, 0x06, 0x08, 0x4A, 0xDB, 0x8B, 0x4C,
      0x9D, 0x47, 0xD7, 0x24, 0x52, 0xF5, 0x3B, 0xA4, 0x6B, 0xED, 0xB1, 0x0B,
      0x6D, 0x7F, 0x19, 0xF7, 0x8E, 0x04, 0xBB, 0xD6, 0xD8, 0xE9, 0x81, 0xD1,
      0x92, 0x4F, 0xDE, 0xD1, 0xB4, 0x84, 0x30, 0x05, 0xDB, 0x53, 0x9C, 0x17,
      0xD4, 0xC2, 0x39, 0x37, 0xEC, 0xAC, 0x30, 0xD8, 0x6D, 0x56, 0x3E, 0x7B,
      0x93, 0x50, 0xF3, 0xA0, 0xC6, 0xF2, 0x0E, 0xAD, 0xF0, 0xFA, 0x73, 0xD9,
      0x3F, 0xE6, 0xB7, 0xBF, 0xC1, 0x41, 0x24, 0x24, 0x86, 0xD0, 0xE9, 0x61,
      0x4D, 0x97, 0x50, 0x50, 0xEB, 0x1F, 0xBF, 0x06, 0x56, 0x82, 0xD1, 0xF8,
      0x0D, 0x06, 0xC8, 0xE8, 0xA6, 0x12, 0x92, 0x28, 0x54, 0x47, 0x0B, 0x29,
      0x20, 0xE0, 0x9B, 0x80, 0xBD, 0x54, 0x60, 0x14, 0x7C, 0x80, 0x25, 0x08,
      0x01, 0x2C, 0x4A, 0x76, 0x13, 0xC3, 0x51, 0x0F, 0x67, 0x3E, 0x2F, 0xC1,
      0x25, 0x20, 0x5E, 0x8E, 0xA3, 0x4D, 0xDB, 0x4A, 0x20, 0x94, 0xDA, 0xF3,
      0x70, 0x30, 0xF0, 0x3D};

  std::vector<uint64_t> expected{
      359103,  27788,  537180,  80920,  725330,  370152, 388319,  746132,
      1030624, 356921, 27686,   691452, 435638,  385832, 317702,  806449,
      161989,  516215, 550534,  134702, 1045515, 51748,  884348,  649807,
      807283,  937248, 791645,  214201, 506516,  931773, 14622,   641120,
      31370,   560722, 399942,  503213, 827866,  162366, 426127,  998937,
      158673,  392043, 548655,  143783, 642074,  453126, 33965,   756556,
      644221,  468050, 1004474, 289773, 725174,  884505, 1013984, 310230,
      888472,  119186, 327149,  111748, 196701,  742300, 97612,   145719,
      969411,  55405,  353255,  758608, 997900,  455182, 712463,  685017,
      261739,  507841, 266818,  296656, 955924,  890704, 331441,  1031942,
      354349,  129037, 27790,   566802, 598661,  280331, 168462,  39808,
      775494,  5244,   524880,  524588, 304993,  246609, 63091,   929729,
      152069,  954019, 318900,  663700, 896823,  12528,  249856};
  auto result = util::ConvertBytesToCoeffs(logt, offset, size, bytes);
  ASSERT_EQ(expected, result);
}

}  // namespace psi::spiral::util
